from socket import *
from dict_db import *
HOST='127.0.0.1'
PORT=8888
ADDR=(HOST,PORT)
#specifically send requests to the server
class Handle:
    def __init__(self):
        self.sock=self._connect()
        self.db=Dict()
    pass

    def _connect(self):
        sock=socket()
        sock.connect(ADDR)
        return sock

    def register(self,user,password):
        request=f'R\t{user}\t{password}'
        self.sock.send(request.encode())
        response=self.sock.recv(128)
        if response == b"T":
            print('registration successful')
            return True
        else:
            print('registration failed')

        pass

    def login(self,user,password):
        request=f'L\t{user}\t{password}'
        self.sock.send(request.encode())
        response=self.sock.recv(128)
        if response == b"T":
            print('login successful')
            return True
        else:
            print('login failed')
        pass

    def query_word(self):
        while True:
            word=input('enter the word you wanna query,press enter or ## to finish the query:')
            if not word or word=='##':
                return
            request=f'r\t{word}'
            self.sock.send(request.encode())
            response=self.sock.recv(128)
            if response:
                print(response.decode())
            else:
                print('not found')

    def viewhistory(self):
        username=f'H\t'
        self.sock.send(username.encode())
        response=self.sock.recv(2048)
        print(response.decode())


        pass


#interacting with users
class DictView:
    def __init__(self):
        self.handle=Handle()
    def _menu_1(self):
        while True:
            print('''
    ============Query=============
    1.Login 2.Register  3.log out 
    ==============================
                ''')
            cmd = input('Enter the operation to be excuted:')
            if cmd == '1':
                user = input('Enter username:')
                password = input('Enter password:')
                if self.handle.login(user,password):
                    self._menu_2()

            elif cmd == '2':
                user=input('Enter username:')
                password = input('Enter password:')
                if self.handle.register(user,password):
                    self._menu_2()
            elif cmd == '3':
                break
            else:
                print('please enter the correct option')


    def _menu_2(self):
        while True:
            print('''
============Query=================
1.Query word  2.History  3.log off 
==================================
            ''')
            cmd = input('Enter the operation to be excuted:')
            if cmd == '1':

                self.handle.query_word()
                self._menu_2()
            elif cmd == '2':
                self.handle.viewhistory()
            elif cmd == '3':
                break
            else:
                print('please enter the correct option')

if __name__ == '__main__':
    dict=DictView()
    dict._menu_1()